//
//  WXOriginalStatusView.swift
//  weibo
//
//  Created by 王旭 on 16/4/20.
//  Copyright © 2016年 王旭. All rights reserved.

//----原创微博自定义View----

import UIKit
import SnapKit
import YYText

class WXOriginalStatusView: UIView {
    
    //顶部控件约束
    private var bottomConstraint : Constraint?

    
    var statusViewModel : WXStatusViewModel?{
        didSet{
            //设置
            homeIconImage.sd_setImageWithURL(NSURL(string: statusViewModel?.status?.user?.profile_image_url ?? ""), placeholderImage: UIImage(named:"avatar_default_small" ))
            nameLabel.text = statusViewModel?.status?.user?.screen_name
            memberImage.image = statusViewModel?.memberImage
            timeLabel.text = statusViewModel?.createAtText
            sourceLabel.text = statusViewModel?.source
//            contentLabel.text = statusViewModel?.status?.text
            contentLabel.attributedText = statusViewModel?.originalAttributedString
            
            //给配图传递数据
            //卸载约束
            bottomConstraint?.uninstall()
            if let pic_urls = statusViewModel?.status?.pic_urls where pic_urls.count > 0 {
                
                
                //是否是一张图片
//                if pic_urls.count == 1 {
//                    let layout = originPhotoView.collectionViewLayout as! UICollectionViewFlowLayout
//                    layout.itemSize = statusViewModel!.orginPhotoSize
//                    print(layout.itemSize)
//                }else{
//                    let layout = originPhotoView.collectionViewLayout as! UICollectionViewFlowLayout
//                    layout.itemSize = CGSize(width: PhotoWH, height: PhotoWH)
//                }
                
                //有配图
//                //1.传递数据
//                originPhotoView.pic_urls = pic_urls
//                //2.更新约束
//                originPhotoView.snp_updateConstraints(closure: { (make) -> Void in
//                    make.size.equalTo(statusViewModel!.orginPhotoSize)
//                })
//                
                originPhotoView.dataInfo = (pic_urls,(statusViewModel!.orginPhotoSize))
                
                //3.显示配图
                originPhotoView.hidden = false
                
                
                self.snp_updateConstraints(closure: { (make) -> Void in
                    bottomConstraint = make.bottom.equalTo(originPhotoView).offset(HomeCellMargin).constraint
                })
            }else{
                //无配图
                //1.更新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    bottomConstraint = make.bottom.equalTo(contentLabel).offset(HomeCellMargin).constraint
                })
                //2.隐藏配图
                originPhotoView.hidden = true
            }
            
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
//        backgroundColor = RandomColor
        
        //添加控件
        addSubview(homeIconImage)
        addSubview(nameLabel)
        addSubview(memberImage)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(verifiedImageView)
        addSubview(originPhotoView)
        
        //约束
        homeIconImage.snp_makeConstraints { (make) -> Void in
            make.top.leading.equalTo(HomeCellMargin)
            make.size.equalTo(CGSizeMake(IconImageWH, IconImageWH))
        }
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(homeIconImage)
            make.leading.equalTo(homeIconImage.snp_trailing).offset(HomeCellMargin)
        }
        memberImage.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(nameLabel)
            make.leading.equalTo(nameLabel.snp_trailing).offset(HomeCellMargin)
        }
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel)
            make.bottom.equalTo(homeIconImage)
        }
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(timeLabel)
            make.leading.equalTo(timeLabel.snp_trailing).offset(HomeCellMargin)
        }
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(homeIconImage)
            make.top.equalTo(homeIconImage.snp_bottom).offset(HomeCellMargin)
        }
        verifiedImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(homeIconImage.snp_trailing).offset(-2)
            make.centerY.equalTo(homeIconImage.snp_bottom).offset(-2)
        }
        originPhotoView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom).offset(HomeCellMargin)
            make.leading.equalTo(contentLabel)
            make.size.equalTo(CGSize(width: 80, height: 80))
        }
        self.snp_makeConstraints { (make) -> Void in
           bottomConstraint = make.bottom.equalTo(originPhotoView).offset(HomeCellMargin).constraint
        }
    }
    
    //懒加载
    //头像
    private lazy var homeIconImage : UIImageView = UIImageView(image: UIImage(named: "avatar_default_small"))
    //昵称
    private lazy var nameLabel : UILabel = {
        let lb = UILabel(textColor: UIColor.darkGrayColor(), fontSize: NameFontSize)
        lb.text = "哈哈"
        return lb
    }()
    //会员图片
    private lazy var memberImage : UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    //时间
    private lazy var timeLabel : UILabel = {
        let timeLb = UILabel(textColor: UIColor.orangeColor(), fontSize: TimeFontSize)
        timeLb.text = "刚刚"
        return timeLb
    }()
    //来源
    private lazy var sourceLabel : UILabel = {
        let sourceLb = UILabel(textColor: UIColor.blackColor(), fontSize: SourceFontSize)
        sourceLb.text = "来自 钻石客户端"
        return sourceLb
    }()
    //正文
    private lazy var contentLabel : YYLabel = {
//        let contentLabel = UILabel(textColor: UIColor.darkGrayColor(), fontSize: ContentFontSize, preferredMaxLayoutWidth: SCREEN_W - 2*HomeCellMargin)
        let contentLabel = YYLabel()
        contentLabel.userInteractionEnabled = true
        contentLabel.textColor = UIColor.darkGrayColor()
        contentLabel.font = UIFont.systemFontOfSize(ContentFontSize)
        contentLabel.preferredMaxLayoutWidth = SCREEN_W - 2 * HomeCellMargin
        contentLabel.numberOfLines = 0
        return contentLabel
    }()
    // 认证
    private lazy var verifiedImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    //原创微博图片
    private lazy var originPhotoView: WXPhotoCollectionView = WXPhotoCollectionView(frame: CGRectZero, collectionViewLayout: UICollectionViewFlowLayout())

}
